<template>
  <div class="app-container">
    <div class="searchContianer" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}"> 
      <div class="searchLeft" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}">
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="68px">
          <el-form-item label="服务类型" prop="serverTypeId" class="mb8">
            <el-select v-model="queryParams.serverTypeId" placeholder="请输入服务类型" clearable size="small">
              <el-option label="全部" value="" />
              <el-option v-for="un in serverTypes" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
          </el-form-item>
          
          <el-form-item label="产品类型" prop="productCategroyId" class="mb8">
            <el-select v-model="queryParams.productCategroyId" placeholder="请输入产品类型" clearable size="small">
              <el-option label="全部" value="" />
              <el-option v-for="un in productCategorys" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
          </el-form-item>
          
        
          <el-form-item label="产品名称" prop="productName" class="mb8">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-form>
        
      </div>
      <div class="searchRight">
        <div style="margin-bottom:20px;margin-top:5px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>

    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vanger:settingRepairFee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vanger:settingRepairFee:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['vanger:settingRepairFee:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" border :data="settingRepairFeeList" @selection-change="handleSelectionChange"
    	 :default-sort="defaultSort" @sort-change="handleSortChange" :height="bigTableData">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="服务类型" align="left" prop="serverTypeName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="产品类型" align="left" prop="productCategroyName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="产品名称" align="left" prop="productName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="标准定价(元/次)金额" align="left" prop="amount" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="保内收费" align="left" prop="isChargeInserver" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.isChargeInserver"/>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="left" fixed="right" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['vanger:settingRepairFee:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['vanger:settingRepairFee:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修费设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="showDetail">
        <el-form-item label="服务类型" prop="serverTypeId">
          <el-select v-model="form.serverTypeId" placeholder="请输入服务类型" clearable >
            <el-option v-for="un in serverTypes" :value="un.id" :label="un.name" :key="un.id"/>
          </el-select>
        </el-form-item>
        
        <el-form-item label="产品类型" prop="productCategroyId">
            <el-select v-model="form.productCategroyId" placeholder="请输入产品类型" clearable @change="getProductList">
              <el-option v-for="un in productCategorys" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
            <el-select v-model="form.productId" placeholder="请输入产品" clearable >
              <el-option v-for="un in products" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
        </el-form-item>
      
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="保内收费" prop="isChargeInserver">          
          <el-radio-group v-model="form.isChargeInserver">
            <el-radio
              v-for="dict in dict.type.sk_yes_no"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        
      

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!showDetail">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSettingRepairFee, listUnion, getSettingRepairFee, getInfoUnion, delSettingRepairFee, addSettingRepairFee, updateSettingRepairFee, exportSettingRepairFee } from "@/api/vanger/settingRepairFee";
import { getServerTypes, getProductCategorys,getProducts } from "@/api/vanger/skcommon";

export default {
  name: "SettingRepairFee",
  dicts: ['sk_common_status', 'sk_yes_no'],
  data() {
    return {
      // 表格高度
      bigTableData:'',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 维修费设置表格数据
      settingRepairFeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查看详情
      showDetail: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 查询参数
      queryParams: {
        params: {},
        pageNum: 1,
        pageSize: 10,
        serverTypeId: null,
        serverTypeName: null,
        productCategroyId: null,
        productCategroyName: null,
        productId: null,
        productName: null,
        amount: null,
        isChargeInserver: null,
        esort: null,
        status: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCategroyId: [
            { required: true }
          ],
        serverTypeId: [
            { required: true }
          ],
        productId: [
            { required: true }
          ],
        isChargeInserver: [
            { required: true }
          ],
        amount: [
            { required: true }
          ]
      },


      //服务类型
      serverTypes: [],
      //产品类型
      productCategroys: [],
        //产品
      products: []

    };
  },
  created() {
    this.getServerTypeList();
    this.getProductCategoryList();
    this.handleSortChange(this.defaultSort);
  },
  mounted() {
    this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
  },
  watch: {
    showSearch() {
      this.$nextTick(function () {
        this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
      })
    }
  },
  methods: {


    //加载所有产品信息
    getProductList(){
      getProducts(this.form.productCategroyId).then(response => {
        this.products = response.data;
      });
    },

    getServerTypeList(){
      getServerTypes().then(response => {
        this.serverTypes = response.data;
      });
    },

    getProductCategoryList(){
      getProductCategorys().then(response => {
        this.productCategorys = response.data;
      });
    },

  
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 查询维修费设置列表 */
    getList() {
      this.loading = true;
      listSettingRepairFee(this.queryParams).then(response => {
        this.settingRepairFeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.closeDialog()
    },
	// 关闭时设置
    closeDialog() {
      this.open = false;
      this.showDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serverTypeId: null,
        serverTypeName: null,
        productCategroyId: null,
        productCategroyName: null,
        productId: null,
        productName: null,
        amount: null,
        isChargeInserver: null,
        esort: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {};
      this.resetForm("queryForm");
      // this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修费设置";
      this.form.isChargeInserver = 1;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSettingRepairFee(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修费设置";
      });
    },
    
    /** 查看按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getSettingRepairFee(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看维修费设置";
        this.showDetail = true;
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSettingRepairFee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSettingRepairFee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('确认删除该条信息吗？').then(function() {
        return delSettingRepairFee(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vanger/settingRepairFee/export', {
        ...this.queryParams
      }, `settingRepairFee_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
